package cn.yi.newsys.service.impl;

import java.util.List;

import cn.yi.newsys.dao.UserDao;
import cn.yi.newsys.dao.impl.UserDaoImpl;
import cn.yi.newsys.pojo.PageBean;
import cn.yi.newsys.pojo.User;
import cn.yi.newsys.service.UserService;

/**
 * 用户业务逻辑实现类
 * 
 * @author Lenovo
 * 
 */
public class UserServiceImpl implements UserService {

	// 添加数据访问层的引用
	private UserDao ud = new UserDaoImpl();

	
	@Override
	public int registerUser(User user) {
		int num = -1 ;
		try {
			User temp = ud.getUserByName(user) ;
			if(temp == null){
				num = ud.save(user) ;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return num ;
	}


	@Override
	public User login(User user) {
		User temp = null ;
		try {
			temp = ud.getUserByName(user) ;
			if(temp != null && temp.getUserPass().equals(user.getUserPass())){
				return temp ;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	@Override
	public List<User> getAllUser() {
		return ud.getAllUser() ;
	}


	@Override
	public int queryTotalCount(String queryName, Integer roleId) {
		return ud.queryTotalCount(queryName, roleId) ;
	}


	@Override
	public PageBean<User> queryAllUser(int currentPage, int pageSize) {
		// 创建 PageBean 对象
		PageBean<User> pb = new PageBean<User>() ;
		// 计算偏移值
		int offset = (currentPage - 1) * pageSize ;
		// 设置当前页码
		pb.setCurrPageNo(currentPage) ;
		// 设置每页显示的记录数
		pb.setPageSize(pageSize) ;
		// 设置当页数据
		pb.setLisr(ud.queryAllUser(offset, pageSize)) ;
		return pb ;
	}


	@Override
	public int getTotalPage(int pageSize,String queryName, Integer roleId) {
		int totalPage = 0 ;
		// 获取总数据数
		int total = this.queryTotalCount(queryName,roleId) ;
		if(total > 0){
			int temp = total / pageSize ;
			totalPage = total % pageSize == 0 ? temp : temp + 1 ;
		}
		return totalPage ;
	}


	@Override
	public PageBean<User> queryUserByPageNo(int pageNo, int pageSize,
			String queryName, Integer roleId) {
		// 获取偏移量
		int offset = (pageNo - 1) * pageSize ;
		// 创建 pageBean 对象
		PageBean<User> pb = new PageBean<User>() ;
		// 获取总页数
		int totalPage = this.getTotalPage(pageSize,queryName, roleId) ;
		// 获取总记录数
		int totalCount = this.queryTotalCount(queryName,roleId) ;
		// 获取当页用户信息
		List<User> list = ud.queryUserByPageNo(offset, pageSize, queryName, roleId) ;
		// 赋值
		pb.setCurrPageNo(pageNo) ;
		pb.setLisr(list) ;
		pb.setPageSize(pageSize) ;
		pb.setTotalCount(totalCount) ;
		return pb ;
	}


	@Override
	public boolean checkUserExists(String userName) {
		User temp = null ;
		try {
			User user = new User() ;
			user.setUserName(userName) ;
			temp = ud.getUserByName(user) ;
			if(temp != null){
				return true ;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}


	@Override
	public boolean checkUserEmailExists(String email) {
		User temp = null ;
		try {
			temp = ud.queryUserByEmail(email) ;
			if(temp != null){
				return true ;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

}
